{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fasttext\n",
    "from sklearn import metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Warning : `load_model` does not return WordVectorModel or SupervisedModel any more, but a `FastText` object which is very similar.\n"
     ]
    }
   ],
   "source": [
    "model = fasttext.load_model(\"model-16.bin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('shuf-test-fasttext.txt') as fopen:\n",
    "    test_X = fopen.read().split('\\n')[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████| 4320/4320 [00:05<00:00, 776.86it/s]\n"
     ]
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "batch_size = 128\n",
    "predicted_Y, actual_Y = [], []\n",
    "\n",
    "for i in tqdm(range(0, len(test_X), batch_size)):\n",
    "    index = min(i + batch_size, len(test_X))\n",
    "    x = test_X[i: index]\n",
    "    batch_x = [' '.join(s.split()[1:]) for s in x]\n",
    "    batch_y = [s.split()[0].replace('__label__', '') for s in x]\n",
    "    results = model.predict(batch_x)[0]\n",
    "    predicted_Y.extend([r[0].replace('__label__', '') for r in results])\n",
    "    actual_Y.extend(batch_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(predicted_Y) == len(actual_Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                        precision    recall  f1-score   support\n",
      "\n",
      "         local-english    0.88328   0.87926   0.88127     50429\n",
      "           local-malay    0.93159   0.92648   0.92903     59877\n",
      "        local-mandarin    0.62000   0.95044   0.75045     49820\n",
      "              manglish    0.98494   0.98157   0.98325     49648\n",
      "                 other    0.99168   0.92850   0.95905     64350\n",
      "socialmedia-indonesian    0.97626   0.95390   0.96495     75140\n",
      "      standard-english    0.86918   0.88018   0.87465     49776\n",
      "   standard-indonesian    0.99695   0.99713   0.99704     50148\n",
      "        standard-malay    0.92292   0.94851   0.93554     50049\n",
      "     standard-mandarin    0.90855   0.53587   0.67413     53709\n",
      "\n",
      "              accuracy                        0.89953    552946\n",
      "             macro avg    0.90853   0.89818   0.89494    552946\n",
      "          weighted avg    0.91425   0.89953   0.89893    552946\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\n",
    "    metrics.classification_report(\n",
    "        actual_Y,\n",
    "        predicted_Y,\n",
    "        digits = 5\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
